//
//  EFBCompanyChartDB.m
//  Pods
//
//  Created by E.F.B on 15-5-26.
//
//

#import "EFBCompanyChartDB.h"
#import "EFBChartDataBaseUtil.h"
#import "ChartContext.h"

@implementation EFBCompanyChartDB

+ (NSArray *)getAllCompanyChartsInfo
{
    __block NSMutableArray *array = [NSMutableArray array];
    FMDatabaseQueue *dbQueue = [EFBChartDataBaseUtil getChartDatabase];
    [dbQueue inDatabase:^(FMDatabase *db)
     {
         if (![db open]) {
             NSLog(@"Could not open DB.");
             return ;
         }
         
         // kind of experimentalish.
         [db setShouldCacheStatements:YES];
         
         if ( ![db tableExists:@"companyCharts_info"] )
         {
             return ;
         }
         FMResultSet *dataRs = nil;
         dataRs = [db executeQuery:[NSString stringWithFormat:@"select * from %@",@"companyCharts_info"]];
         
         while ([dataRs next])
         {
             [array addObject:[dataRs resultDictionary] ];
         }
         
         [dataRs close];
         [db close];
     }];
    return array;
}

+ (NSMutableArray *)getTheCompanyChartsByCode4:(NSString *)code4
{
    __block NSMutableArray *array = [NSMutableArray array];
    FMDatabaseQueue *dbQueue = [EFBChartDataBaseUtil getChartDatabase];
    [dbQueue inDatabase:^(FMDatabase *db)
     {
         if (![db open]) {
             NSLog(@"Could not open DB.");
             return ;
         }
         
         // kind of experimentalish.
         [db setShouldCacheStatements:YES];
         
         if ( ![db tableExists:@"companyCharts_info"] )
         {
             return ;
         }
         FMResultSet *dataRs = nil;
         if (code4){
             dataRs = [db executeQuery:@"select * from companyCharts_info where code4 = ?",code4];
//             dataRs = [db executeQuery:@"select * from companyCharts_info"];
         }
         
         while ([dataRs next])
         {
             [array addObject:[dataRs resultDictionary] ];
         }
         
         [dataRs close];
         [db close];
     }];
    return array;
}

+ (NSMutableArray *)getTheCompantChartsByCode4:(NSString *)code4 andSubsidiary:(NSString *)subsidiary
{
    __block NSMutableArray *array = [NSMutableArray array];
    FMDatabaseQueue *dbQueue = [EFBChartDataBaseUtil getChartDatabase];
    [dbQueue inDatabase:^(FMDatabase *db)
     {
         if (![db open]) {
             NSLog(@"Could not open DB.");
             return ;
         }
         
         // kind of experimentalish.
         [db setShouldCacheStatements:YES];
         
         if ( ![db tableExists:@"companyCharts_info"] )
         {
             return ;
         }
         FMResultSet *dataRs = nil;
         if (code4){
             dataRs = [db executeQuery:@"select * from companyCharts_info where code4 = ? and subsidiary = ?", code4, subsidiary];
//             dataRs = [db executeQuery:@"select * from companyCharts_info where subsidiary = ?", subsidiary];
         }
         
         while ([dataRs next])
         {
             [array addObject:[dataRs resultDictionary] ];
         }
         
         [dataRs close];
         [db close];
     }];
    return array;
}

@end
